﻿using Microsoft.Extensions.Logging;
using System;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Json;
using Xms.Abp.Data.Message;

namespace Xms.Abp.Smsing
{
    public class NullTemplateSmsSenderProvider: TemplateSmsSenderProviderBase
    {
        protected IJsonSerializer JsonSerializer { get; }
        protected ILogger Logger { get; }
        public NullTemplateSmsSenderProvider(
            SmsSettings smsSettings,
            SupplierSmsSettings supplierSmsSettings,
            IJsonSerializer jsonSerializer,
            ILogger<NullTemplateSmsSenderProvider> logger)
            : base(smsSettings, supplierSmsSettings)
        {
            JsonSerializer = jsonSerializer;
            Logger = logger;
        }

        public override string ProviderName => "Null";

        public override Task<SentResult<SmsSentResultItem>> SendSmsAsync(SmsMessage smsMessage)
        {
            Logger.LogWarning($"SMS Sending was not implemented! Using {nameof(NullSmsSender)}:");

            Logger.LogWarning("TenantId          : " + smsMessage.TenantId);
            Logger.LogWarning("Phone Numbers     : " + string.Join(",", smsMessage.PhoneNumbers));
            Logger.LogWarning("TemplateCode      : " + smsMessage.TemplateCode);
            Logger.LogWarning("Template          : " + smsMessage.Template);
            if (smsMessage.TemplateParameters != null)
            {
                Logger.LogWarning("TemplateParameters: " + JsonSerializer.Serialize(smsMessage.TemplateParameters));
            }

            return Task.FromResult(new SentResult<SmsSentResultItem>
            {
                ProviderName = ProviderName,
                RequestId = Guid.NewGuid().ToString(),
                Sign = null,
                Items = smsMessage.PhoneNumbers.Select(r => new SmsSentResultItem
                {
                    PhoneNumber = r,
                    BizId = Guid.NewGuid().ToString(),
                    ErrorCode = "OK",
                    Message = "OK",
                    SentStatus = SentStatus.Success
                })
            });
        }
    }
}
